﻿#region Usings

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Rodunu.Movies.Contracts;
using Rodunu.Movies.Interfaces.Activity;
using Rodunu.Movies.Interfaces.Comments;
using Rodunu.Movies.Interfaces.Movie;
using Rodunu.Movies.Interfaces.User;
using Rodunu.Movies.Logic.Comments;
using Rodunu.Movies.Logic.Movie;
using Rodunu.Movies.Logic.User;

#endregion

namespace Rodunu.Movies.Web.Utility
{
    public class ActivityBuilder
    {
        #region Private Members

        private readonly UserInfo _user;
        private readonly IUserActivity _userActivityLogic;
        private readonly StringBuilder _activityString;
        private bool _isMovie = false;
        private bool _isCurrentUser;

        #endregion

        #region Constructor

        public ActivityBuilder(UserInfo user, IUserActivity userActivity)
        {
            _user = user;
            _userActivityLogic = userActivity;
            _activityString = new StringBuilder();
        }

        #endregion

        #region Properties

        public string Height { get; set; }

        #endregion

        #region Public Methods

        public string GetFriendActivity()
        {
            var userActivities = _userActivityLogic.GetActivityForFriends(_user.Id);

            _activityString.Append("<table id='FriendActivity' cellpadding='0' cellspacing='0' class='FrontPage'>");
            int counter = 0;
            foreach (var activity in userActivities)
            {
                _activityString.Append("<tr>");
                SetAvatar(activity);
                SetName(activity);
                _activityString.Append("</tr>");

                _activityString.Append("<tr>");
                SetActivity(activity);
                _activityString.Append("</tr>");

                _activityString.Append("<tr id='FreindRow");
                _activityString.Append(activity.ActivityId);
                _activityString.Append("'>");
                SetActivityTime(activity);
                _activityString.Append("</tr>");

                _activityString.Append("<tr class='tablecomments'>");
                _activityString.Append("<td id='Comments");
                _activityString.Append(activity.ActivityId);
                _activityString.Append("'>");
                if (_isMovie)
                    _activityString.Append(GetComments(activity.ActivityId));
                _activityString.Append("</td></tr>");

                counter++;
            }
            _activityString.Append("</table>");
            Height = (counter * 100) + "px";
            return _activityString.ToString();
        }

        public string GetUserActivity()
        {
            var userActivities = _userActivityLogic.GetActivityForUser(_user.Id);
            _isCurrentUser = true;
            _activityString.Append("<table id='FriendActivity' cellpadding='0' cellspacing='0' class='FrontPage'>");
            int counter = 0;
            foreach (var activity in userActivities)
            {
                _activityString.Append("<tr>");
                SetActivity(activity);
                _activityString.Append("</tr>");

                _activityString.Append("<tr id='FreindRow");
                _activityString.Append(activity.ActivityId);
                _activityString.Append("'>");
                SetActivityTime(activity);
                _activityString.Append("</tr>");

                _activityString.Append("<tr class='tablecomments'>");

                if (_isCurrentUser)
                    _activityString.Append("<td style='padding-left: 20px' id='Comments");
                else
                    _activityString.Append("<td id='Comments");

                _activityString.Append(activity.ActivityId);
                _activityString.Append("'>");
                if (_isMovie)
                    _activityString.Append(GetComments(activity.ActivityId));
                _activityString.Append("</td></tr>");

                counter++;
            }
            _activityString.Append("</table>");
            Height = (counter * 100) + "px";
            return _activityString.ToString();
        }

        #endregion

        #region Private Functions

        private static string GetComments(long userMovieId)
        {
            IComments commentsLogic = new CommentsLogic();
            IList<Comments> allcomments = commentsLogic.GetCommentsByUserMovie(userMovieId);
            var sb = new StringBuilder();
            if (allcomments.Count > 0)
            {
                foreach (var comments in allcomments)
                {
                    sb.Append(CommentsHelper.BuildSmallComment(comments, false));
                }
            }
            return sb.ToString();
        }
        
        private void SetActivity(UserActivity activity)
        {
            if (_isCurrentUser)
                _activityString.Append("<td class='activity' style='padding-top: 4px'>You ");
            else
                _activityString.Append("<td class='activity'>");
            switch (activity.ActivityType)
            {
                case (int)UserActivityTypes.AddFriend:
                    _activityString.Append("added friend ");
                    IFriends friendsLogic = new FriendsLogic();
                    Friends friends = friendsLogic.Find(activity.ActivityId);
                    if (friends.Friend.ShowProfile)
                    {
                        _activityString.Append("<a href='../User/UserInfo.ashx?userName=");
                        _activityString.Append(friends.Friend.UserName);
                        _activityString.Append("'>");
                        _activityString.Append(friends.Friend.UserName);
                        _activityString.Append("</a>");
                    }
                    else
                        _activityString.Append(friends.Friend.UserName);
                    _isMovie = false;
                    break;
                case (int)UserActivityTypes.AddMovie:
                    _activityString.Append("added ");
                    GetMovie(activity.ActivityId);
                    _isMovie = true;
                    break;
                case (int)UserActivityTypes.EditMovie:
                    _activityString.Append("edited ");
                    GetMovie(activity.ActivityId);
                    _isMovie = true;
                    break;
                case (int)UserActivityTypes.RemoveMovie:
                    _activityString.Append("removed ");
                    GetMovie(activity.ActivityId);
                    _isMovie = false;
                    break;
                case (int)UserActivityTypes.UpdateProfile:
                    _activityString.Append("updated profile.");
                    _isMovie = false;
                    break;
            }
            _activityString.Append("</td>");
        }

        private void SetName(UserActivity activity)
        {
            _activityString.Append("<td class='Title'>");
            if (activity.UserInfo.ShowProfile)
            {
                _activityString.Append("<a href='../User/UserInfo.ashx?userName=");
                _activityString.Append(activity.UserInfo.UserName);
                _activityString.Append("'>");
                _activityString.Append(activity.UserInfo.UserName);
                _activityString.Append("</a>");
            }
            else
                _activityString.Append(activity.UserInfo.UserName);
            _activityString.Append("</td>");
        }

        private void SetAvatar(UserActivity activity)
        {
            _activityString.Append("<td class='avatar' rowspan='4'>");
            if (string.IsNullOrEmpty(activity.UserInfo.Avatar))
                _activityString.Append("<img id='UserAvatar' style='max-width: 55px; max-height: 55px' src='../Content/img/blank_avatar.jpg' />");
            else
            {
                _activityString.Append("<img id='UserAvatar' style='max-width: 55px; max-height: 55px' src='../Uploads/");
                _activityString.Append(activity.UserInfo.Id);
                _activityString.Append(@"/Avatars/");
                _activityString.Append(activity.UserInfo.Avatar);
                _activityString.Append("' >");
            }
            _activityString.Append("</td>");
        }

        private void SetActivityTime(UserActivity activity)
        {
            if (_isCurrentUser)
                _activityString.Append("<td class='Bottom' style='padding: 2px 0 0 10px'>");
            else
                _activityString.Append("<td class='Bottom'>");

            _activityString.Append("<div class='Time'>");
            if (DateTime.Now.Subtract(activity.DateAdded).Days > 7)
            {
                _activityString.Append(activity.DateAdded.ToShortDateString());
            }
            else
            {
                if (DateTime.Now.Subtract(activity.DateAdded).Days == 0)
                {
                    if (DateTime.Now.Subtract(activity.DateAdded).Hours == 0)
                    {

                        _activityString.Append(DateTime.Now.Subtract(activity.DateAdded).Minutes);
                        _activityString.Append(" minutes ago");
                    }
                    else
                    {
                        _activityString.Append(DateTime.Now.Subtract(activity.DateAdded).Hours);
                        _activityString.Append(" hours ago");
                    }
                }
                else
                {
                    _activityString.Append(DateTime.Now.Subtract(activity.DateAdded).Days);
                    _activityString.Append(" days ago");
                }
            }
            _activityString.Append("</div>");
            if (_isMovie)
                _activityString.Append(SetLinks(activity));
            _activityString.Append("</td>");
        }
        
        private string SetLinks(UserActivity activity)
        {
            IUserMovies userMoviesLogic = new UserMoviesLogic();
            UserMovies userMovies = userMoviesLogic.Find(activity.ActivityId);

            var sb = new StringBuilder();
            sb.Append("<div class='Links'>");
            sb.Append(@"<div onclick=""AddComment('");
            sb.Append(userMovies.Id);
            sb.Append(@"','");
            sb.Append(_user.Id);
            sb.Append(@"')"">Comment</div>");
            //sb.Append("<div>Rate Movie</div>");

            if (userMovies.Movie != null)
            {
                var myMovie = userMoviesLogic.OwnMovie(_user.Id, userMovies.Movie.Id);
                if (myMovie == null || myMovie.Id == 0)
                {
                    //sb.Append("<div>Write a Review</div>");
                    sb.Append(@"<div class='spacer'>|</div><div onclick=""AddToMyCollection('");
                    sb.Append(userMovies.Name);
                    sb.Append("','");
                    sb.Append(userMovies.Movie.Id);
                    sb.Append("','");
                    sb.Append(userMovies.Movie.Name);
                    sb.Append(@"');"">Add to my collection</div>");
                    //onclick="AskToBorrow('$userMovie.Id', '$userMovie.UserInfo.Id', this)"
                    sb.Append(@"<div class='spacer'>|</div><div style='border-right: none' onclick=""AskToBorrow('");
                    sb.Append(userMovies.Id);
                    sb.Append("','");
                    sb.Append(userMovies.UserInfo.Id);
                    sb.Append(@"','this');"">Ask to Borrow</div>");
                }
            }
            sb.Append("</div>");
            return sb.ToString();
        }

        private void GetMovie(long movieId)
        {
            IUserMovies movies = new UserMoviesLogic();
            var movie = new UserMovies();
            try
            {
                movie = movies.Find(movieId);
            }
            catch (Exception)
            {
                //TODO: log exception
                return;
            }
            if (movie != null && movie.Id > 0)
            {
                _activityString.Append("<a href='../Movie/Detail.ashx?id=");
                _activityString.Append(movie.Id.ToString());
                try
                {
                    if (movie.UserInfo != null)
                    {
                        _activityString.Append("&UserId=");
                        _activityString.Append(movie.UserInfo.Id.ToString());
                    }
                }
                catch (Exception)
                {

                    //TODO: log exception
                }
                _activityString.Append("'>");
                try
                {
                    _activityString.Append(movie.Name);
                }
                catch (Exception)
                {
                    //TODO: log exception

                }
                _activityString.Append("</a>");
            }
        }

        #endregion

    }
}
